<?php
/*
	后台奖品控制器
*/

if(!defined('BASEPATH')) exit('No direct script access allowed');

Class Weixin_Actioninfo_prize_module extends CI_Module
{
	private $aid;					//活动id
	private $uid;					//登录用户ID
	public $sid;
	public $adminid;
	
	public function __construct()
    { 
        parent::__construct();
		$this->sid = $GLOBALS['sid'];
		$this->load->model('weixin.weixin_model');
        $this->load->model('expert.Expert_model');
		$this->load->library("pagination");
        $this->setting = $this->weixin_model->db_get_setting();
        define('TOKEN', @$this->setting['token']);
        define('APPID', @$this->setting['appid']);
        define('APPSECRET', @$this->setting['appsecret']);
        $this->adminid = $this->session->userdata('admin_user_id');
        if (!$this->adminid) {
            redirect(site_url('admin/index/login'));
        }
		$id=$this->uri->segment(4,0);
		$this->aid=$id?$id:'';									//没有页面传参数，默认为6活动		    
		//echo $this->aid;
		$this->load->model('weixin.actioninfo_prize_model');			//载入模型
		$this->load->model('weixin.actioninfo_model'); 
    }

	function index($aid=0,$id=''){
		$allactioninfo=$this->actioninfo_model->sel_activ($this->sid);	//所有活动
		$pagenum=15;
		$allnum=$this->actioninfo_prize_model->num_product($this->aid,$this->sid);
		/*
		$config['total_rows']=$allnum;   		//CI配置文件 总共的条数
		$config['per_page']=$pagenum;			//CI配置文件，一页显示条数
		$config['num_links']=4;					//当前页前后要显示的几个分页连接
		$config['base_url']=site_url("/weixin/actioninfo_prize/index/".$aid);
		$config['use_page_numbers']=true;


		//调用分类页
		$this->load->library("pagination");
		$this->pagination->initialize($config);
		$productlist['pagelists']=$this->pagination->create_links()."<br/>";
		*/
		$config['uri_segment'] = 5;
		$config['total_rows']=$allnum;   		//CI配置文件 总共的条数
		$config['per_page']=$pagenum;			//CI配置文件，一页显示条数
		$config['num_links']=3;					//当前页前后要显示的几个分页连接

		$config['base_url']=site_url("/weixin/actioninfo_prize/index/".$aid);
		$config['use_page_numbers']=true;
		$config['first_link'] = '第一页';
		$config['last_link'] = '最后一页';
		//调用分类页
		$this->pagination->initialize($config);
		$productlist['pagelists']=$this->pagination->create_links()."<br/>";

		$id=$id?$id:1;
		$startpage=($id-1)*$pagenum;			//第几页从第几条开始显示
		$productlist['list']=$this->actioninfo_prize_model->select_jion($startpage,$pagenum,$this->aid,$this->sid);			//倒序排列
		#endregin  分页完成
		$productlist['view']="action/actioninfo_prize_view";
		$productlist['allaction']=$allactioninfo;
		$productlist['title']="奖品列表";
		$this->loadview($productlist, 'xenon/common/index');
	}
	
	function show_add(){								//奖品添加页
		//数组 包含活动id和名字
		$arractiv=array('aid','aname');
		$productlist['arrlist']=$this->actioninfo_model->sel_key($arractiv,$this->sid);
		$productlist['view']='action/actioninfo_prize_add_view';
		$productlist['title']='添加奖品';
		$productlist['index_url']="/weixin/actioninfo_prize/insert_add";
		$this->loadview($productlist, 'xenon/common/index');
	}
	
	//添加记录
	function insert_add(){
		$aids=$_POST['selactiv'];
		foreach($_POST['pname'] as $key=>$vals){
			if(!empty($_POST['pname'][$key])){
				$val='';
				$val['aid'] = $aids;
				$val['sid'] = $this->sid;
				$val['pname'] = $_POST['pname'][$key];
				$val['pnumb'] = $_POST['pnum'][$key];
				$val['pchange'] = $_POST['pchan'][$key];
				$val['ptype'] = $_POST['ptype'][$key];
				$randcode = '';
					for($i=1;$i<11;$i++){
						$randcode.=rand(0,9);
					}
				$val['pcode'] = $randcode;
				$val['pspecial'] = $_POST['special'][$key];
				$this->actioninfo_prize_model->add_product($val);	
			}
		}
		echo"<script>alert('奖品添加成功'); window.location='".site_url('/weixin/actioninfo_prize/index')."';</script>";
	}
	
	//显示编辑
	function show_edit(){
		$arractiv=array('aid','aname');
		$productlist['arrlist']=$this->actioninfo_model->sel_key($arractiv,$this->sid);
		$arrs=$this->actioninfo_prize_model->sel_one($this->aid);
		$productlist['list']=$arrs;
		$productlist['view']='action/actioninfo_prize_edit_view';
		$productlist['title']='编辑';
		$productlist['index_url']="/weixin/actioninfo_prize/edit_up/".$this->aid;
		$this->loadview($productlist, 'xenon/common/index');
	}
	
	//修改记录
	function edit_up(){
		$randcode="";
		for($i=1;$i<11;$i++){
			$randcode.=rand(0,9);
		}
		$val=array("aid"=>$_POST['selactiv'],
		"pname"=>$_POST['pname'],
		"pnumb"=>$_POST['pnum'],
		"pchange"=>$_POST['pchan'],
		"ptype"=>$_POST['ptype'],
		"pcode"=>$randcode
		);
		$this->actioninfo_prize_model->update_one($this->aid,$val);
		echo"<script>alert('修改成功'); window.location='".site_url('/weixin/actioninfo_prize/index')."';</script>";
	}	
		
	//删除  连接得到ID，url的第三段
	function product_del(){
		$this->actioninfo_prize_model->delete_one($this->aid);
		echo"<script>alert('删除成功'); window.location='".site_url('/weixin/actioninfo_prize/index')."';</script>";
	}
	
}
?>